package com.demo.service.impl;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.demo.entity.po.SysMenu;

import com.demo.entity.query.SysMenuQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.service.SysMenuService;

import com.demo.utils.StringTools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.SysMenuMapper;

import com.demo.entity.enums.PageSize;


@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService{

	public static final Integer DEFUALT_ROOT_MENUID = 0;

	private static final String ROOT_MENU_NAME = "所有菜单";


	@Resource
	private SysMenuMapper<SysMenu,SysMenuQuery> sysMenuMapper;



	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<SysMenu> findListByParam(SysMenuQuery param) {
		List<SysMenu> sysMenuList = this.sysMenuMapper.selectList(param);
		if (param.getFormate2Tree() != null && param.getFormate2Tree()) {
			SysMenu root = new SysMenu();
			root.setMenuId(DEFUALT_ROOT_MENUID);
			root.setpId(-1);
			root.setMenuName(ROOT_MENU_NAME);
			sysMenuList.add(root);
			sysMenuList = convertLine2Tree4Menu(sysMenuList, -1);
		}
		return sysMenuList;
	}

	@Override
	public List<SysMenu> convertLine2Tree4Menu(List<SysMenu> dataList, Integer pid) {
		List<SysMenu> children = new ArrayList<>();
		for (SysMenu m : dataList) {
			if (m.getMenuId() != null && m.getpId() != null && m.getpId().equals(pid)) {
				m.setChildren(convertLine2Tree4Menu(dataList, m.getMenuId()));
				children.add(m);
			}
		}
		return children;
	}





	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(SysMenuQuery query){
		return this.sysMenuMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<SysMenu> findListByPage(SysMenuQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<SysMenu> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(SysMenu bean) { 
		return this.sysMenuMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<SysMenu> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysMenuMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<SysMenu> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysMenuMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据MenuId查询
	 */
	public SysMenu getSysMenuByMenuId(Integer menuId) { 
		return this.sysMenuMapper.selectByMenuId(menuId);
	}

	/**
	 * 根据MenuId更新
	 */
	public Integer updateSysMenuByMenuId(SysMenu bean, Integer menuId) { 
		return this.sysMenuMapper.updateByMenuId(bean,menuId);
	}

	/**
	 * 根据MenuId删除
	 */
	public Integer deleteSysMenuByMenuId(Integer menuId) { 
		return this.sysMenuMapper.deleteByMenuId(menuId);
	}

	@Override
	public void saveMenu(SysMenu sysMenu) {
		if (sysMenu.getMenuId() == null) {
			this.sysMenuMapper.insert(sysMenu);
		} else {
			this.sysMenuMapper.updateByMenuId(sysMenu, sysMenu.getMenuId());
		}
	}

	@Override
	public List<SysMenu> getAllMenuByRoleIds(String roleIds) {
		if (StringTools.isEmpty(roleIds)) {
			return new ArrayList<>();
		}
		int[] roleIdArray = Arrays.stream(roleIds.split(",")).mapToInt(Integer::valueOf).toArray();
		return sysMenuMapper.selectAllMenuByRoleIds(roleIdArray);
	}
}